package com.pic.mgt.haishin;

import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;

import com.pic.mgt.HaishinLauncher;
import com.pic.mgt.log.IgxLog;
import com.pic.mgt.unit.Business;

public class HaishinExecutionQuartzJobBean extends QuartzJobBean {
	
	private static HaishinBusinessUnitDispatcher haishinBusinessUnitDispatcher;
	
	static {
		haishinBusinessUnitDispatcher = 
			HaishinLauncher.applicationContext.getBean(
									"haishinBusinessUnitDispatcher",
									HaishinBusinessUnitDispatcher.class);
	}
	
	@Override
	protected void executeInternal(JobExecutionContext jobExecutionContext)
									throws JobExecutionException {
		JobDataMap jobDataMap = 
			jobExecutionContext.getTrigger().getJobDataMap();
		Business business =
			(Business)jobDataMap.get(HaishinScheduler.jobDataBusinessKey);
		
		// ********************** Log **********************
			if (IgxLog.messageLog.isDebugMode()) {
			IgxLog.messageLog.debug(this,
					"Quartz job: Sending haishin business '{}' execution  to haishin dispatcher.",
					business.getUnitName());
		}
		// ********************** Log **********************
		
		haishinBusinessUnitDispatcher.sendToUnitExecuter(business);	
	}
}
